package com.lukebang.controller;

import java.util.List;


import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lukebang.pojo.Territory;
import com.lukebang.service.ITerritoryService;

/**
 * 
 * TerritoryForController 上午10:53:34
 * 
 * Copyright zhaocj Inc. All rights reserved. Love ME Like Justin Bieber.
 */

@Controller
@RequestMapping("/territory/")
public class TerritoryForController {
	private static Logger logger = LogManager.getLogger(TerritoryForController.class.getName());
	@Resource
	ITerritoryService territorySER;

	public ITerritoryService getITerritoryService() {
		return territorySER;
	}

	public void setITerritoryService(ITerritoryService territorySER) {
		this.territorySER = territorySER;
	}

	@RequestMapping("selectAvailableProvince")
	public @ResponseBody List<Territory> selectAvailableProvinceByWorker() {
		long start = System.currentTimeMillis();
		List<Territory> provinceByWorker = null;
		try {
			provinceByWorker = territorySER.selectAvailableProvinceByWorker();
		} catch (Exception e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		logger.debug("消耗了{}毫秒", end - start);
		return provinceByWorker;
	}

	@RequestMapping("selectAvailableCity")
	public @ResponseBody List<Territory> selectAvailableCityByProvince(String provinceCode) {
		List<Territory> cities = null;
		try {
			cities = territorySER.selectAvailableCityByProvince(provinceCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cities;
	}

	// 根据省市查询可用的区域
	@RequestMapping("selectAvailableCountry")
	public @ResponseBody List<Territory> selectAvailableCountry(String cityCode) {
		// 要查询的城市的code
		logger.debug("要查询的城市的code：{}" + cityCode);
		List<Territory> countries = null;
		try {
			countries = territorySER.selectAvailableCountryByCity(cityCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return countries;
	}

	@RequestMapping("selectTerritoryPrice")
	public @ResponseBody Territory selectTerritoryPriceByCity(String provinceCode, String cityCode) {
		logger.debug("要查询的城市的价钱信息:{}/{}", provinceCode, cityCode);
		Territory terrPrice = null;
		try {
			terrPrice = territorySER.selectTerritoryPriceByCity(provinceCode, cityCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return terrPrice;
	}

	// 跳转到区域查询页面
	@RequestMapping("areaSearch")
	public String areaSearch(Model model) {
		model.addAttribute("oneMenu", "taskList");
		model.addAttribute("twoMenu", "areaSearch");
		return "/cms/taskList/areaSearch";
	}

	// 查询所有省份
	@RequestMapping("allProvince")
	public @ResponseBody List<Territory> findAllProvince(Integer level) {
		return territorySER.findAllProvince(level);
	}

	// 查询所有的市
	@RequestMapping("allCity")
	public @ResponseBody List<Territory> findAllTerritory(String parentId) {
		return territorySER.findAllCity(parentId);
	}

	// 查询所有的区
	@RequestMapping("allCountry")
	public @ResponseBody List<Territory> findAllCountry(String code) {
		return territorySER.findAllCountry(code);
	}

	@RequestMapping("/isCovered")
	public @ResponseBody boolean isCovered(String name) {
		System.out.println("要查的区域是     " + name);
		return territorySER.isCovered(name);
	}
	
	//// 查询所选区域覆盖的师父的个数
	@RequestMapping("/isCoveredWorker")
	public @ResponseBody Integer isCoveredWorker(String name) {
		System.err.println("要查的区域所涵盖的师父数量是     " + name);
		return territorySER.selectCountWorkerByName(name);
	}
}
